package com.genexususa.eo.bing.connection;

import com.genexususa.eo.bing.CampaignManagementServiceClasses.*;
import com.genexususa.eo.bing.data.GxException;
import com.genexususa.eo.bing.data.Info;
import com.genexususa.eo.bing.data.Util;
import com.genexususa.eo.bing.data.results.*;
import com.genexususa.eo.bing.data.results.GxGoalResult;
import com.genexususa.eo.bing.data.results.GxIdResult;
import com.genexususa.eo.bing.data.wsSubclasses.GxGoal;
import org.xml.sax.SAXException;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.PropertyException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.rpc.ServiceException;
import java.io.IOException;
import java.io.StringWriter;
import java.rmi.RemoteException;

public class GoalManager {
    public GoalManager() {
    }
    /*
    public String addGoals(String userName, String password, String developerToken, long customerId, long customerAccountId, String applicationToken, long accountId, String goalsXML) {
        String namespace = Info.namespace;
        String url = Info.url;
        ICampaignManagementService campaignManagement = null;
        CampaignManagementServiceLocator campaignServiceLocator = null;
        BasicHttpBinding_ICampaignManagementServiceStub stub = null;
        AddGoalsRequest addGoalsRequest = null;
        AddGoalsResponse addGoalsResponse = null;
        GxGoalResults result = null;
        String trackingId = "";
        Util util = new Util();
        Goal[] goals = null;
        try {
            campaignServiceLocator = new CampaignManagementServiceLocator();
            campaignServiceLocator.setBasicHttpBinding_ICampaignManagementServiceEndpointAddress(url);
            campaignManagement = campaignServiceLocator.getBasicHttpBinding_ICampaignManagementService(); // ServiceException
            stub = (BasicHttpBinding_ICampaignManagementServiceStub) campaignManagement;

            // Set header parameters
            stub.setHeader(namespace, "UserName", userName);
            stub.setHeader(namespace, "Password", password);
            stub.setHeader(namespace, "DeveloperToken", developerToken);
            stub.setHeader(namespace, "CustomerId", customerId);
            stub.setHeader(namespace, "CustomerAccountId", customerAccountId);
            stub.setHeader(namespace, "ApplicationToken", applicationToken);

            // Set body parameters
            addGoalsRequest = new AddGoalsRequest();
            addGoalsRequest.setAccountId(accountId);
            // Parse goals from XML
            goals = util.goalsFromXML(goalsXML); // IllegalArgumentException, GxException, ParserConfigurationException, SAXException, IOException
            addGoalsRequest.setGoals(goals);

            // Call WS
            addGoalsResponse = campaignManagement.addGoals(addGoalsRequest); // RemoteException, AnalyticsApiFaultDetail, AdApiFaultDetail

            // Call succeeded with no errors

            // Create the return object
            trackingId = stub.getResponseHeader(namespace, "TrackingId").getValue();
            GoalResult[] goalResults = addGoalsResponse.getGoalResults();
            result = new GxGoalResults(trackingId, goalResults.length);
            for (int i=0; i<goalResults.length; i++) {
                result.addResult(i, new GxGoalResult(((GxGoal)goals[i]), goalResults[i]));
            }
        } catch (ServiceException e) { // BEFORE CALL
            result = new GxGoalResults(trackingId);
            result.addServiceExceptionError(e);
        } catch (IllegalArgumentException e) { // BEFORE CALL
            result = new GxGoalResults(trackingId);
            result.addIllegalArgumentExceptionError(e);
        } catch (GxException e) { // BEFORE CALL
            result = new GxGoalResults(trackingId, e.getExceptions().length);
            for (int i=0; i<e.getExceptions().length; i++) {
                result.addResult(i, new GxGoalResult());
            }
            result.addGxExceptionError(e);
        } catch (ParserConfigurationException e) { // BEFORE CALL
            result = new GxGoalResults(trackingId);
            result.addParserConfigurationExceptionError(e);
        } catch (SAXException e) { // BEFORE CALL
            result = new GxGoalResults(trackingId);
            result.addSAXExceptionError(e);
        } catch (ApiFaultDetail fault) { // IN CALL
            trackingId = fault.getTrackingId();
            if (fault.getBatchErrors().length > 0) {
                result = new GxGoalResults(trackingId, goals.length);
                for (int i=0; i<goals.length; i++) {
                    result.addResult(i, new GxGoalResult((GxGoal)goals[i]));
                }
            } else {
                result = new GxGoalResults(trackingId);
            }
            result.addApiFaultDetailError(fault);
        } catch (AdApiFaultDetail fault) { // IN CALL
            trackingId = fault.getTrackingId();
            result = new GxGoalResults(trackingId);
            result.addAdApiFaultDetailError(fault);
        } catch (AnalyticsApiFaultDetail fault) { // IN CALL
            trackingId = fault.getTrackingId();
            if (fault.getGoalErrors().length > 0) {
                result = new GxGoalResults(trackingId, goals.length);
                for (int i=0; i<goals.length; i++) {
                    result.addResult(i, new GxGoalResult((GxGoal)goals[i]));
                }
            } else {
                result = new GxGoalResults(trackingId);
            }
            result.addAnalyticsApiFaultDetailError(fault);
        } catch (RemoteException e) { // IN CALL
            result = new GxGoalResults(trackingId);
            e.printStackTrace();
            result.addRemoteExceptionError(e);
        } catch (IOException e) { // BEFORE CALL
            result = new GxGoalResults(trackingId);
            result.addIOExceptionError(e);
        }

        String returnMessage = null;
        // GxGoalResults -> XML
        try {
            JAXBContext contextObj = JAXBContext.newInstance(GxGoalResults.class); // JAXBException

            Marshaller marshallerObj = contextObj.createMarshaller(); // JAXBException
            marshallerObj.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); // PropertyException

            StringWriter sw = new StringWriter();

            marshallerObj.marshal(result, sw); // JAXBException
            returnMessage = sw.getBuffer().toString();
        }  catch (PropertyException e) { // AFTER CALL
            returnMessage = util.getPropertyExceptionMessage(e);
        } catch (JAXBException e) { // AFTER CALL
            returnMessage = util.getJAXBExceptionMessage(e);
        }
        return returnMessage;
    }

    public String updateGoals(String userName, String password, String developerToken, long customerId, long customerAccountId, String applicationToken, long accountId, String goalsXML) {
        String namespace = Info.namespace;
        String url = Info.url;
        ICampaignManagementService campaignManagement = null;
        CampaignManagementServiceLocator campaignServiceLocator = null;
        BasicHttpBinding_ICampaignManagementServiceStub stub = null;
        UpdateGoalsRequest updateGoalsRequest = null;
        UpdateGoalsResponse updateGoalsResponse = null;
        GxGoalResults result = null;
        String trackingId = "";
        Util util = new Util();
        Goal[] goals = null;
        try {
            campaignServiceLocator = new CampaignManagementServiceLocator();
            campaignServiceLocator.setBasicHttpBinding_ICampaignManagementServiceEndpointAddress(url);
            campaignManagement = campaignServiceLocator.getBasicHttpBinding_ICampaignManagementService(); // ServiceException
            stub = (BasicHttpBinding_ICampaignManagementServiceStub) campaignManagement;

            // Set header parameters
            stub.setHeader(namespace, "UserName", userName);
            stub.setHeader(namespace, "Password", password);
            stub.setHeader(namespace, "DeveloperToken", developerToken);
            stub.setHeader(namespace, "CustomerId", customerId);
            stub.setHeader(namespace, "CustomerAccountId", customerAccountId);
            stub.setHeader(namespace, "ApplicationToken", applicationToken);

            // Set body parameters
            updateGoalsRequest = new UpdateGoalsRequest();
            updateGoalsRequest.setAccountId(accountId);
            // Parse goals from XML
            goals = util.goalsFromXML(goalsXML); // IllegalArgumentException, GxException, ParserConfigurationException, SAXException, IOException
            updateGoalsRequest.setGoals(goals);

            // Call WS
            updateGoalsResponse = campaignManagement.updateGoals(updateGoalsRequest); // RemoteException, AnalyticsApiFaultDetail, ApiFaultDetail, AdApiFaultDetail

            // Call succeeded with no errors

            // Create the return object
            trackingId = stub.getResponseHeader(namespace, "TrackingId").getValue();
            GoalResult[] goalResults = updateGoalsResponse.getGoalResults();
            result = new GxGoalResults(trackingId, goalResults.length);
            for (int i=0; i<goalResults.length; i++) {
                result.addResult(i, new GxGoalResult(((GxGoal)goals[i]), goalResults[i]));
            }
        } catch (ServiceException e) { // BEFORE CALL
            result = new GxGoalResults(trackingId);
            result.addServiceExceptionError(e);
        } catch (IllegalArgumentException e) { // BEFORE CALL
            result = new GxGoalResults(trackingId);
            result.addIllegalArgumentExceptionError(e);
        } catch (GxException e) { // BEFORE CALL
            result = new GxGoalResults(trackingId, e.getExceptions().length);
            for (int i=0; i<e.getExceptions().length; i++) {
                result.addResult(i, new GxGoalResult());
            }
            result.addGxExceptionError(e);
        } catch (ParserConfigurationException e) { // BEFORE CALL
            result = new GxGoalResults(trackingId);
            result.addParserConfigurationExceptionError(e);
        } catch (SAXException e) { // BEFORE CALL
            result = new GxGoalResults(trackingId);
            result.addSAXExceptionError(e);
        } catch (ApiFaultDetail fault) { // IN CALL
            trackingId = fault.getTrackingId();
            if (fault.getBatchErrors().length > 0) {
                result = new GxGoalResults(trackingId, goals.length);
                for (int i=0; i<goals.length; i++) {
                    result.addResult(i, new GxGoalResult((GxGoal)goals[i]));
                }
            } else {
                result = new GxGoalResults(trackingId);
            }
            result.addApiFaultDetailError(fault);
        } catch (AdApiFaultDetail fault) { // IN CALL
            trackingId = fault.getTrackingId();
            result = new GxGoalResults(trackingId);
            result.addAdApiFaultDetailError(fault);
        } catch (AnalyticsApiFaultDetail fault) { // IN CALL
            trackingId = fault.getTrackingId();
            if (fault.getGoalErrors().length > 0) {
                result = new GxGoalResults(trackingId, goals.length);
                for (int i=0; i<goals.length; i++) {
                    result.addResult(i, new GxGoalResult((GxGoal)goals[i]));
                }
            } else {
                result = new GxGoalResults(trackingId);
            }
            result.addAnalyticsApiFaultDetailError(fault);
        } catch (RemoteException e) { // IN CALL
            result = new GxGoalResults(trackingId);
            result.addRemoteExceptionError(e);
        } catch (IOException e) { // BEFORE CALL
            result = new GxGoalResults(trackingId);
            result.addIOExceptionError(e);
        }

        String returnMessage = null;
        // GxGoalResults -> XML
        try {
            JAXBContext contextObj = JAXBContext.newInstance(GxGoalResults.class); // JAXBException

            Marshaller marshallerObj = contextObj.createMarshaller(); // JAXBException
            marshallerObj.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); // PropertyException

            StringWriter sw = new StringWriter();

            marshallerObj.marshal(result, sw); // JAXBException
            returnMessage = sw.getBuffer().toString();
        }  catch (PropertyException e) { // AFTER CALL
            returnMessage = util.getPropertyExceptionMessage(e);
        } catch (JAXBException e) { // AFTER CALL
            returnMessage = util.getJAXBExceptionMessage(e);
        }
        return returnMessage;
    }

    public String deleteGoals(String userName, String password, String developerToken, long customerId, long customerAccountId, String applicationToken, long accountId, String goalsXML) {
        String namespace = Info.namespace;
        String url = Info.url;
        ICampaignManagementService campaignManagement = null;
        CampaignManagementServiceLocator campaignServiceLocator = null;
        BasicHttpBinding_ICampaignManagementServiceStub stub = null;
        DeleteGoalsRequest deleteGoalsRequest = null;
        GxIdResults result = null;
        String trackingId = "";
        Util util = new Util();
        Goal[] goals = null;
        try {
            campaignServiceLocator = new CampaignManagementServiceLocator();
            campaignServiceLocator.setBasicHttpBinding_ICampaignManagementServiceEndpointAddress(url);
            campaignManagement = campaignServiceLocator.getBasicHttpBinding_ICampaignManagementService(); // ServiceException
            stub = (BasicHttpBinding_ICampaignManagementServiceStub) campaignManagement;

            // Set header parameters
            stub.setHeader(namespace, "UserName", userName);
            stub.setHeader(namespace, "Password", password);
            stub.setHeader(namespace, "DeveloperToken", developerToken);
            stub.setHeader(namespace, "CustomerId", customerId);
            stub.setHeader(namespace, "CustomerAccountId", customerAccountId);
            stub.setHeader(namespace, "ApplicationToken", applicationToken);

            // Set body parameters
            deleteGoalsRequest = new DeleteGoalsRequest();
            deleteGoalsRequest.setAccountId(accountId);
            // Parse goals from XML
            goals = util.goalsFromXML(goalsXML); // IllegalArgumentException, GxException, ParserConfigurationException, SAXException, IOException
            long[] goalIds = new long[goals.length];
            for (int i=0; i<goalIds.length; i++) {
                if (goals[i].getId() == null) {
                    goalIds[i] = -1;
                } else {
                    goalIds[i] = goals[i].getId();
                }
            }
            deleteGoalsRequest.setGoalIds(goalIds);

            // Call WS
            campaignManagement.deleteGoals(deleteGoalsRequest); // RemoteException, AnalyticsApiFaultDetail, ApiFaultDetail, AdApiFaultDetail

            // Call succeeded with no errors

            // Create the return object
            trackingId = stub.getResponseHeader(namespace, "TrackingId").getValue();
            result = new GxIdResults(trackingId, goalIds.length);
            for (int i=0; i<goalIds.length; i++) {
                result.addResult(i, new GxIdResult(((GxGoal)goals[i]).getGxId(), goalIds[i]));
            }
        } catch (ServiceException e) { // BEFORE CALL
            result = new GxIdResults(trackingId);
            result.addServiceExceptionError(e);
        } catch (IllegalArgumentException e) { // BEFORE CALL
            result = new GxIdResults(trackingId);
            result.addIllegalArgumentExceptionError(e);
        } catch (GxException e) { // BEFORE CALL
            result = new GxIdResults(trackingId, e.getExceptions().length);
            for (int i=0; i<e.getExceptions().length; i++) {
                result.addResult(i, new GxIdResult());
            }
            result.addGxExceptionError(e);
        } catch (ParserConfigurationException e) { // BEFORE CALL
            result = new GxIdResults(trackingId);
            result.addParserConfigurationExceptionError(e);
        } catch (SAXException e) { // BEFORE CALL
            result = new GxIdResults(trackingId);
            result.addSAXExceptionError(e);
        } catch (ApiFaultDetail fault) { // IN CALL
            trackingId = fault.getTrackingId();
            if (fault.getBatchErrors().length > 0) {
                result = new GxIdResults(trackingId, goals.length);
                for (int i=0; i<goals.length; i++) {
                    result.addResult(i, new GxIdResult(((GxGoal)goals[i]).getGxId()));
                }
            } else {
                result = new GxIdResults(trackingId);
            }
            result.addApiFaultDetailError(fault);
        } catch (AdApiFaultDetail fault) { // IN CALL
            trackingId = fault.getTrackingId();
            result = new GxIdResults(trackingId);
            result.addAdApiFaultDetailError(fault);
        } catch (RemoteException e) { // IN CALL
            result = new GxIdResults(trackingId);
            result.addRemoteExceptionError(e);
        } catch (IOException e) { // BEFORE CALL
            result = new GxIdResults(trackingId);
            result.addIOExceptionError(e);
        }

        String returnMessage = null;
        // GxIdResults -> XML
        try {
            JAXBContext contextObj = JAXBContext.newInstance(GxIdResults.class); // JAXBException

            Marshaller marshallerObj = contextObj.createMarshaller(); // JAXBException
            marshallerObj.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); // PropertyException

            StringWriter sw = new StringWriter();

            marshallerObj.marshal(result, sw); // JAXBException
            returnMessage = sw.getBuffer().toString();
        }  catch (PropertyException e) { // AFTER CALL
            returnMessage = util.getPropertyExceptionMessage(e);
        } catch (JAXBException e) { // AFTER CALL
            returnMessage = util.getJAXBExceptionMessage(e);
        }
        return returnMessage;
    }
    */
}
